import 'package:flutter/material.dart';
import 'package:flutter_swiper_null_safety/flutter_swiper_null_safety.dart';

class MySwiper extends StatefulWidget {
  const MySwiper({Key? key, required this.list}) : super(key: key);
  final List list;

  @override
  _MySwiperState createState() => _MySwiperState();
}

class _MySwiperState extends State<MySwiper> {
  @override
  Widget build(BuildContext context) {
    return Swiper(
      // 轮播总数
      itemCount: widget.list.length,
      // 轮播项
      itemBuilder: (BuildContext context, int index) {
        return Tooltip( // 长按提示
          message: widget.list[index].title,
          child: Image(
            image: AssetImage(widget.list[index].path),
            fit: BoxFit.fill,
          ),
        );
      },
      // 分页器插件
      pagination: const SwiperPagination(
          alignment: Alignment.bottomCenter,
          margin: EdgeInsets.only(bottom: 20.0),
          // 圆点插件
          builder: DotSwiperPaginationBuilder(
            color: Colors.black54,
            activeColor: Colors.green,
          )
      ),
      // 滚动方向
      scrollDirection: Axis.horizontal,
      // 是否自动滚动
      autoplay: true,
      // 用户操作时停止自动轮播
      autoplayDisableOnInteraction: true,
      // 无限轮播
      loop: true,
      // 点击某一项
      onTap: (index) {
        print(index);
      },
      control: const SwiperControl(
          size: 15.0,
          color: Colors.orange
      ),
      viewportFraction: 1,
      // 缩放
      scale: 1,
    );
  }
}
